﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESPL.CodeGenerator.Common.ProjectBuilders;
using ESPL.CodeGenerator.Common;
using ESPL.CodeGenerator.Common.Structs;
using ESPL.CodeGenerator.Domain.TemplateGenerators.SolutionGenerators;
using ESPL.CodeGenerator.Domain.Utilities;
using ESPL.CodeGenerator.Domain.TemplateGenerators.ProjectGenerators;
using ESPL.CodeGenerator.Common.Enums;
using ESPL.CodeGenerator.Domain.TemplateGenerators.ConfigGenerators;
using ESPL.CodeGenerator.Domain.TemplateGenerators.CSharpCodeGenerators;
using ESPL.CodeGenerator.Domain.TemplateGenerators.ViewGenerators;
using System.IO;

namespace ESPL.CodeGenerator.Domain.ProjectBuilders
{
    internal class MVC5ProjectBuilder : ProjectBuilderBase, IDataProjectBuilder
    {
        #region Constructor

        public MVC5ProjectBuilder(ApplicationConfiguration config, ClassLibraryConfiguration projectConfig)
            : base(config, projectConfig)
        {
            WriteFile = true;
            CreateDir = true;
        }

        #endregion

        #region Methods

        protected override BuilderResponse GetResponse()
        {
            return new BuilderResponse { Output = string.Format("{0} Project Created at: {1}", _generator.ToString(), GetFilePath()) };
        }

        protected override void InitGenerator()
        {
            _generator = new MVCProjectGenerator5(Config,this);
        }

        public override IList<IBuilderResponse> InitializeProjectTree()
        {
            var response =  base.InitializeProjectTree();
            var zipFilePath = string.Format("{0}/Resources/MV5ProjectResources/MV5ProjectResources.zip",this.Config.ResourceFolderPath);
            ZipHelper.UnzipFile(zipFilePath, GetDirectoryPath());
            WriteGlobalAsaxFiles();
            WriteStartupCSharpFile();
            WriteWebConfigFile();
            WriteBundleConfigFile();
            WriteFilterConfigFile();
            WriteRouteConfigFile();
            WriteStartupAuthFile();
            WriteHomeControllerFile();
            WriteHomeControllerIndexViewFile();
            WriteViewStartGeneratorFile();
            WriteViewWebConfigFile();
            return response;
        }

        private void WriteGlobalAsaxFiles()
        {
            var globalAsaxGenerator = new GlobalASAXGenerator(this);
            var filePath = string.Format("{0}/Global.asax", GetDirectoryPath());
            WriteFileContents(filePath, globalAsaxGenerator);

            var globalAsaxCSharpGenerator = new GlobalASAXGeneratorCSharp(this);
            var globalAsaxCSharpFilePath = string.Format("{0}/Global.asax.cs", GetDirectoryPath());
            WriteFileContents(globalAsaxCSharpFilePath, globalAsaxCSharpGenerator);
        }

        private void WriteStartupCSharpFile()
        {
            var startupGenerator = new StartupCSharpGenerator(this);
            var filePath = string.Format("{0}/Startup.cs", GetDirectoryPath());
            WriteFileContents(filePath, startupGenerator);
        }

        private void WriteWebConfigFile()
        {
            var webConfigGenerator = new WebConfigGenerator(this);
            var filePath = string.Format("{0}/Web.config", GetDirectoryPath());
            WriteFileContents(filePath, webConfigGenerator);
        }

        private void WriteBundleConfigFile()
        {
            var bundleConfigGenerator = new BundleConfigGenerator(this);
            var filePath = string.Format("{0}/App_Start/BundleConfig.cs", GetDirectoryPath());
            WriteFileContents(filePath, bundleConfigGenerator);
        }

        private void WriteFilterConfigFile()
        {
            var filterGenerator = new FilterConfigGenerator(this);
            var filePath = string.Format("{0}/App_Start/FilterConfig.cs", GetDirectoryPath());
            WriteFileContents(filePath, filterGenerator);
        }

        private void WriteRouteConfigFile()
        {
            var RouteGenerator = new RouteConfigGenerator(this);
            var filePath = string.Format("{0}/App_Start/RouteConfig.cs", GetDirectoryPath());
            WriteFileContents(filePath, RouteGenerator);
        }

        private void WriteStartupAuthFile()
        {
            var startupAuthGenerator = new StartupAuthGenerator(this);
            var filePath = string.Format("{0}/App_Start/Startup.Auth.cs", GetDirectoryPath());
            WriteFileContents(filePath, startupAuthGenerator);
        }

        private void WriteHomeControllerFile()
        {
            var homeControllerGenerator = new HomeControllerGenerator(this);
            var filePath = string.Format("{0}/Controllers/HomeController.cs", GetDirectoryPath());
            WriteFileContents(filePath, homeControllerGenerator);
        }

        private void WriteHomeControllerIndexViewFile()
        {
            CreateDirectory(string.Format("{0}/Views/Home", GetDirectoryPath()));
            var indexViewGenerator = new HomeControllerIndexViewGenerator(this);
            //Temp
            var filePath = string.Format("{0}/Views/Home/Index.cshtml", GetDirectoryPath());
            WriteFileContents(filePath, indexViewGenerator);
        }

        private void WriteViewStartGeneratorFile()
        {
            CreateDirectory(string.Format("{0}/Views/Shared", GetDirectoryPath()));
            var indexViewGenerator = new ViewStartGenerator(this);
            var filePath = string.Format("{0}/Views/Shared/_ViewStart.cshtml", GetDirectoryPath());
            WriteFileContents(filePath, indexViewGenerator);
        }

        private void WriteViewWebConfigFile()
        {
            var generator = new ViewsWebConfigGenerator(this);
            var filePath = string.Format("{0}/Views/Web.config", GetDirectoryPath());
            WriteFileContents(filePath, generator);
        }
        #endregion
    }
}
